package com.qianqu.module_finding.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.qianqu.module_finding.R;

/**
 * ${description}
 *
 * @author weiqiliu
 * @version 1.0 2018/6/26
 */
public class CircleBmiView extends View {
    private Bitmap bitmap;
    private Bitmap bitmap_san;
    private Bitmap bitmap_inner;
    private Bitmap bitmap_inner_shadow;
    private Resources mResources;
    private Paint circlePaint;
    private static int innerR;//内部圆环的半径
    private int circleBitmapWidth, circleBitmapHeight;
    private int sanBitmapWidth, sanBitmapHeight;
    private int innerBitmapWidth, innerBitmapHeight;
    private int innerShadowBitmapWidth, innerShadowBitmapHeight;
    private Rect circleSrcRect, circleDesRect;
    private Rect san_SrcRect, san_DesRect;
    private Rect innerSrcRect, innerDesRect;
    private Rect innerShadowSrcRect, innerShadowDesRect;
    private float bmiNum = 12.0f;
    private int topTextSize;
    private int middleTextSize;
    private int blowTextSize;
    private int topTextInterval;
    private int middleTextInterval;
    private int blowTextInterval;
    private float angle;

    public CircleBmiView(Context context) {
        super(context);
        initPaint();
        initBitmap();
    }

    public CircleBmiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
        initPaint();
        initBitmap();
    }

    public CircleBmiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        initPaint();
        initBitmap();
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleBmiView);
        bmiNum = typedArray.getFloat(R.styleable.CircleBmiView_bmi_num, 20);
        if (bmiNum > 37.00) {
            bmiNum = 37.0f;
        }
        if (bmiNum < 12.0f) {
            bmiNum = 12.0f;
        }
        topTextSize = typedArray.getDimensionPixelSize(R.styleable.CircleBmiView_top_text_size, 14);
        middleTextSize = typedArray.getDimensionPixelSize(R.styleable.CircleBmiView_middle_text_size, 41);
        blowTextSize = typedArray.getDimensionPixelSize(R.styleable.CircleBmiView_blow_text_size, 14);
        topTextInterval = typedArray.getDimensionPixelSize(R.styleable.CircleBmiView_top_text_interval, 14);
        middleTextInterval = typedArray.getDimensionPixelSize(R.styleable.CircleBmiView_middle_text_interval, 41);
        blowTextInterval = typedArray.getDimensionPixelSize(R.styleable.CircleBmiView_blow_text_interval, 14);
    }

    private void initPaint() {
        if (circlePaint == null) {
            circlePaint = new Paint();
            circlePaint.setStyle(Paint.Style.STROKE);
            circlePaint.setColor(Color.BLACK);
            circlePaint.setStrokeWidth(10f);
        }
        mResources = getResources();
    }

    private void initBitmap() {
        bitmap = ((BitmapDrawable) mResources.getDrawable(R.mipmap.bmi_circle)).getBitmap();
        bitmap_san = ((BitmapDrawable) mResources.getDrawable(R.mipmap.bmi_point)).getBitmap();
        bitmap_inner = ((BitmapDrawable) mResources.getDrawable(R.mipmap.bmi_inner_circle)).getBitmap();
        bitmap_inner_shadow = ((BitmapDrawable) mResources.getDrawable(R.mipmap.bmi_inner_shadow_circle)).getBitmap();
        circleBitmapWidth = bitmap.getWidth();
        circleBitmapHeight = bitmap.getHeight();
        sanBitmapWidth = bitmap_san.getWidth();
        sanBitmapHeight = bitmap_san.getHeight();
        innerBitmapWidth = bitmap_inner.getWidth();
        innerBitmapHeight = bitmap_inner.getHeight();
        innerShadowBitmapWidth = bitmap_inner_shadow.getWidth();
        innerShadowBitmapHeight = bitmap_inner_shadow.getHeight();
        innerR = bitmap_inner.getHeight();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        circleSrcRect = new Rect(0, 0, circleBitmapWidth, circleBitmapHeight);
        circleDesRect = new Rect(0, 0, circleBitmapWidth, circleBitmapHeight);
        san_SrcRect = new Rect(0, 0, sanBitmapWidth, sanBitmapHeight);
        san_DesRect = new Rect(0, 0, sanBitmapWidth, sanBitmapHeight);
        innerSrcRect = new Rect(0, 0, innerBitmapWidth, innerBitmapHeight);
        innerDesRect = new Rect(0, 0, innerBitmapWidth, innerBitmapHeight);
        innerShadowSrcRect = new Rect(0, 0, innerShadowBitmapWidth, innerShadowBitmapHeight);
        innerShadowDesRect = new Rect(0, 0, innerShadowBitmapWidth, innerShadowBitmapHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制基本图标
        canvas.drawBitmap(bitmap, circleSrcRect, circleDesRect, null);
        canvas.translate((circleBitmapWidth - innerBitmapWidth) / 2, circleBitmapHeight - innerBitmapHeight);
        canvas.drawBitmap(bitmap_inner, innerSrcRect, innerDesRect, null);
        canvas.translate(-(circleBitmapWidth - innerBitmapWidth) / 2, -(circleBitmapHeight - innerBitmapHeight));
        canvas.translate((circleBitmapWidth - innerShadowBitmapWidth) / 2, circleBitmapHeight - innerShadowBitmapHeight);
        canvas.drawBitmap(bitmap_inner_shadow, innerShadowSrcRect, innerShadowDesRect, null);
        canvas.translate(-(circleBitmapWidth - innerShadowBitmapWidth) / 2, -(circleBitmapHeight - innerShadowBitmapHeight));
        //绘制文字
        canvas.translate(circleBitmapWidth / 2, circleBitmapHeight);
        float changeX;
        float changeY;
        canvas.translate(-500 / 2, -(innerR - topTextInterval));
        changeX = 500 / 2;
        changeY = (innerR - topTextInterval);
        drawText(canvas, topTextSize, 0xFFB0B3B1, "BMI");
        canvas.translate(0, middleTextInterval);
        changeY = changeY - middleTextInterval;
        drawText(canvas, middleTextSize, 0xFF333333, bmiNum + "");
        canvas.translate(0, blowTextInterval);
        changeY = changeY - blowTextInterval;
        drawText(canvas, blowTextSize, 0xFF333333, dmidescribe(bmiNum));
        canvas.translate(changeX, changeY);
        //绘制箭头
        bmiNum=27.0f;
        if (bmiNum <= 18.5) {
            angle = (float) ((bmiNum - 12) / (18.5 - 12) * 60.0);
        } else if (bmiNum <= 24) {
            angle = (float) ((bmiNum - 18.5) / (24.0 - 18.5) * 60.0 + 60);
        } else {
            angle = (float) ((bmiNum - 24) / (38.0 - 24.0) * 60.0 + 120);
        }
        float degrees = angle - 90;
        float radians = (float) Math.toRadians(degrees);
        canvas.translate((float) (Math.sin(radians) * (innerR + sanBitmapHeight + 1)), -(float) (Math.cos(radians) * (innerR + sanBitmapHeight + 1)));
        canvas.rotate(degrees);
        canvas.drawBitmap(bitmap_san, - sanBitmapWidth / 2,  0, null);
    }
//
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        setMeasuredDimension(circleBitmapWidth, circleBitmapHeight);
//    }

    private void drawText(Canvas canvas, int size, int color, String testString) {
        Rect targetRect = new Rect(0, 0, 500, size + 20);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(3);
        paint.setTextSize(size);
        paint.setColor(0x0000FFFF);
        canvas.drawRect(targetRect, paint);
        paint.setColor(color);
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(testString, targetRect.centerX(), baseline, paint);
    }

    private String dmidescribe(float dmi) {
        String dmidescribe = "";
        if (dmi < 18.5) {
            dmidescribe = "体型偏瘦";
        } else if (dmi < 24 && dmi >= 18.5) {
            dmidescribe = "体型正常";
        } else if (dmi < 27 && dmi >= 24) {
            dmidescribe = "体型偏胖";
        } else if (dmi < 30 && dmi >= 27) {
            dmidescribe = "体型肥胖";
        } else if (dmi >= 30) {
            dmidescribe = "重度肥胖";
        }
        return dmidescribe;
    }

    public void refreshDmi(float bmiNum){
        this.bmiNum=bmiNum;
        invalidate();
    }

}
